package com.yunkaweilai.android.activity.member;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.member.IntegralChangeModel;
import com.yunkaweilai.android.model.member.MemberHeadModel;
import com.yunkaweilai.android.net.Glide.GlideUtils;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.popup.ImageEnlagePopup;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * 积分变动
 * Created by Administrator on 2017/6/30.
 */

public class IntegralChangeActivity extends BaseActivity implements BGARefreshLayout.BGARefreshLayoutDelegate {

    @BindView(R.id.id_img_head)
    ImageView idImgHead;
    @BindView(R.id.id_tv_name)
    TextView idTvName;
    @BindView(R.id.id_tvcard_number)
    TextView idTvcardNumber;
    @BindView(R.id.id_tv_card_infor)
    TextView idTvCardInfor;
    @BindView(R.id.id_tv_all_stored)
    TextView idTvAllStored;
    @BindView(R.id.id_listView)
    ListView idListView;
    @BindView(R.id.content_view)
    BGARefreshLayout contentView;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;


    public static final String Integral_MEMBER_MODEL = "Integral_MEMBER_MODEL";
    public static final String Integral_MEMBER_BUNDLE = "Integral_MEMBER_BUNDLE";
    private ArrayList<IntegralChangeModel.DataBean.ListBean> memberList = new ArrayList<>();
    private CommonAdapter<IntegralChangeModel.DataBean.ListBean> adapter;
    private int mpage = 1;
    private boolean mHasMore = true;
    private MemberHeadModel model;

    public static void actionStart(Context context, MemberHeadModel model) {
        Intent intent = new Intent(context, IntegralChangeActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(Integral_MEMBER_MODEL,model);
        intent.putExtra(Integral_MEMBER_BUNDLE,bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_integral_change);
        ButterKnife.bind(this);
        idMultipleStatusView.showLoading();
        model = (MemberHeadModel) getIntent().getBundleExtra(Integral_MEMBER_BUNDLE).getSerializable(Integral_MEMBER_MODEL);
        initRefreshLayout(contentView, this, true);
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                idMultipleStatusView.showLoading();
                initHttp(true);
            }
        });
        initHeadView();
        initAdapter();
        initHttp(true);
        initClick();
    }

    private void initClick() {
        idImgHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.ImageEnlage(mAc,"1",model.getMember_avatar(),model.getMember_sex());
            }
        });
    }

    private void initHeadView() {
        new TitleBuilder(mAc).setTitleText("积分变动").setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Utils.MemberHeadImg(mAc,model.getMember_avatar(),model.getMember_sex(),idImgHead);
        idTvName.setText(model.getMember_name() + " (" + model.getLevel_name() + ": " + model.getCard_no() + ")");
        idTvCardInfor.setText("折扣：" + model.getDiscount() /10 + "折   积分：" + model.getMember_points() + "   余额：" + model.getMember_price());
    }


    private boolean initHttp(final boolean refresh) {
        mpage = refresh ? 1 : mpage;
        if ((!mHasMore) && (!refresh)) {
            contentView.endLoadingMore();
            showToast("没有更多数据了");
            return false;
        }
        HttpRequest.postUrl(Api.INTEGRAL_CHANGE_MEMBER)
                .addParams("ShowPage", "1")
                .addParams("PageSize", "10")
                .addParams("CurrPage", mpage + "")
                .addParams("MemberId", model.getId())
                .log(TAG)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        endRefreshOrLoad(refresh);
                        Gson gson = new Gson();
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode) {
                            mpage++;
                            IntegralChangeModel storedModel = gson.fromJson(json, IntegralChangeModel.class);
                            if (refresh) {
                                memberList.clear();
                            }

                            if (storedModel.getData().getList() != null && storedModel.getData().getList().size() != 0) {
                                memberList.addAll(storedModel.getData().getList());
                                adapter.notifyDataSetChanged();
                                mHasMore = storedModel.getData().getPage().isNext();//是否还有下一页
                                idTvAllStored.setText("共" + storedModel.getData().getPage().getCount() + "次");
                                if (idMultipleStatusView != null) {
                                    idMultipleStatusView.showContent();
                                }
                            } else {
                                mHasMore = false;
                                if (refresh) {
                                    if (idMultipleStatusView != null) {
                                        idTvAllStored.setText("共0次");
                                        idMultipleStatusView.showEmpty();
                                    }
                                }
                            }
                        } else {
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null) {
                            idMultipleStatusView.showError();
                        }
                    }
                });


        return true;
    }

    /**
     * 结束刷新组件
     *
     * @param refresh
     */
    private void endRefreshOrLoad(boolean refresh) {
        if (refresh) {
            if (contentView != null) {
                contentView.endRefreshing();
            }
        } else {
            if (contentView != null) {
                contentView.endLoadingMore();
            }
        }
    }

    private void initAdapter() {
        adapter = new CommonAdapter<IntegralChangeModel.DataBean.ListBean>(mAc, R.layout.item_list_integral_change, memberList) {
            @Override
            protected void convert(ViewHolder viewHolder, IntegralChangeModel.DataBean.ListBean item, int position) {
                viewHolder.setText(R.id.id_tv_now_integral,item.getX_points());
                viewHolder.setText(R.id.id_tv_integral_change,item.getPoints() +"(" + item.getSource() + ")");
                viewHolder.setText(R.id.id_tv_time,item.getCreate_time() + "");

            }
        };
        idListView.setAdapter(adapter);

    }


    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        initHttp(true);
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return initHttp(false);
    }
}
